//
//  ViewController.m
//  UI15_UICollectionViewLifeCycle
//
//  Created by Hanlingyu on 16/8/3.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define kCollectionViewReuse @"reuse"
#define kColor [UIColor colorWithRed:arc4random() % 256 / 255.0  green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]
@interface ViewController ()

<
    UICollectionViewDelegate,
    UICollectionViewDataSource
>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.bounds.size.width - 15) / 2, 120);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collection.backgroundColor = [UIColor whiteColor];
    collection.dataSource = self;
    collection.delegate = self;
    [self.view addSubview:collection];

    [collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    // 关于collectonView LifeCyle
    // 1. numberOfSectionsInCollectionView:
    // 2.collectionView:numberOfItemsInSection:
    // 注意: 3-4循环执行
    // 3. prepareForReuse(从重用池取出时调用)
    // 4. collectionView:cellForItemAtIndexPath:
    // 5. CollectionView:willDisPlayCell
    // 当cell完全出屏幕时
    // 6. collectionView:didEndDisplayingCell
    // iOS 10以前
    // 出屏幕立即返回, 依旧进入3->4->5
    // iOS 10以后
    // 出屏幕立即返回, 会执行4->5, 不会执行3, 提高了效率
    
}
#pragma mark - collection
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

// 区里的行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"-------------[%@ : %s]", self, __FUNCTION__);

    return 100;
    
}
// 从chongyongchisu9su899
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.backgroundColor = kColor;
    
    NSLog(@"-------------[%@ : %s]", self, __FUNCTION__);

    return cell;
}

// iOS8.以后推出, cell将要先显示在屏幕
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%@----------------%ld-------------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
    
    
}
// iOS8.以后推出, cell已经离开在屏幕

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----------------%ld-------------[%@ : %s]", cell, indexPath.row, self, __FUNCTION__);

    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
